package manager;

import java.util.List;
import org.hibernate.Session;

import util.HibernateUtil;

public class ReportManager {

	@SuppressWarnings("unchecked")
	public Object[] getMostUsedModel() {
		try {
			List<Object[]> mostUsed = null;
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			mostUsed = session
					.createQuery(
							"select brand, count(*) from PrisTeam1Car group by brand order by count(*) desc")
					.list();
			session.getTransaction().commit();
			Object[] result = mostUsed.get(0);
			return result;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static void main(String[] args) {
		ReportManager rm = new ReportManager();
		Object[] mostUsed = rm.getMostUsedModel();
		if (mostUsed != null && mostUsed.length > 0)
			System.out.println("Most used brand: " + mostUsed[0].toString()
					+ ", count: " + mostUsed[1].toString());
	}
}
